﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;
using System.Threading;
namespace TS_XY_Train_Repaire.UserCon.Stuff
{
    public partial class uc_Department_Position_Relation_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "";
        public string _LoadName = "";
        public uc_Department_Position_Relation_ManaForm()
        {
            InitializeComponent();
        }

        private void uc_Department_Position_Relation_ManaForm_Load(object sender, EventArgs e)
        {
            this.Text = _LoadName+"->>关联职位管理";
            _LoadOrgList();
            _LoadAuthGrid();
        }
        #region//--组织结构--//
        private void _LoadOrgList()
        {
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            tl_Main.Nodes.Clear();
            tl_Main.Refresh();
            ArrayList pList = new ArrayList();
            string strSQL = "select *,(select count(*) from TS_S_T_P_Dep_Position where f_depid=" + _LoadID.Substring(1) + " and f_posid=TS_S_T_P_Position.f_id and f_del=0) as f_count from TS_S_T_P_Position where f_deep=0 and f_rsid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + " and f_rbid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau + " and f_del=0 order by f_id asc";
            string _strDepQuery = " and (";
            int i = 0;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                if (i == 0)
                {
                    _strDepQuery += "(f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                else
                {
                    _strDepQuery += " or (f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "P" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                if (read["f_count"].ToString() == "0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                _ls.f_parent = "H";
                _ls.f_imgindex = 4;
                pList.Add(_ls);
                i++;
            }
            _strDepQuery += ")";
            if (_strDepQuery.Length > 10)
            {
                read.Close();
                strSQL = "select *,(select count(*) from TS_S_T_P_Dep_Position where f_depid=" + _LoadID.Substring(1) + " and f_posid=TS_S_T_P_Position.f_id and f_del=0) as f_count from TS_S_T_P_Position where f_rsid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + " and f_rbid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayBureau + " " + _strDepQuery + " and f_deep>0 and f_del=0 order by f_id asc";
                cmd = new SqlCommand(strSQL, conn);
                read = cmd.ExecuteReader();
                while (read.Read())
                {
                    _ls = new LeverString_SysOrganization();
                    _ls.f_id = "P" + read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    _ls.f_code = read["f_code"].ToString();
                    _ls.f_deep = read["f_deep"].ToString();
                    _ls.f_level = read["f_level"].ToString();
                    _ls.f_rbid = read["f_rbid"].ToString();
                    _ls.f_rsid = read["f_rsid"].ToString();
                    string _UPID = "";
                    _UPID = read["f_level"].ToString();
                    _UPID = _UPID.Substring(0, _UPID.Length - 1);
                    _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                    _ls.f_parent = "P" + _UPID;
                    _ls.f_imgindex = 4;
                    if (read["f_count"].ToString() == "0")
                    {
                        _ls.f_ck = false;
                    }
                    else
                    {
                        _ls.f_ck = true;
                    }
                    pList.Add(_ls);
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();

            tl_Main.DataSource = pList;
        }
        #endregion
        private void _LoadAuthGrid()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,*,(select count(*) from TS_S_T_P_Stuff where f_depid=" + _LoadID.Substring(1) + " and f_posid=TS_S_T_P_Dep_Position.f_posid and f_del=0) as f_typename,(select f_name from TS_S_T_P_Position where f_id=TS_S_T_P_Dep_Position.f_posid) as f_funcname,convert(bit,0) as f_ck from TS_S_T_P_Dep_Position where f_depid=" + _LoadID.Substring(1) + " and f_del=0";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.OK;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _FuncList = GetFunclIst(tl_Main.Nodes, "");
            TS_Win_Class.MySqlCom("update TS_S_T_P_Dep_Position set f_del=1,f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_depid=" + _LoadID.Substring(1)+" and f_del=0");
            if (_FuncList.Length > 0)
            {
                string[] _arry = _FuncList.Split(',');
                for (int i = 0; i < _arry.Length; i++)
                {
                    if (_arry[i].Length > 0)
                    {
                        strSQL += "insert into TS_S_T_P_Dep_Position(f_depid,f_posid,f_workerid) values(" + _LoadID.Substring(1) + "," + _arry[i].Substring(1) + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ");";
                    }
                }
                TS_Win_Class.MySqlCom(strSQL);
                Thread.Sleep(500);
                _LoadAuthGrid();
            }
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string _str = "";
            bool _hasStuff = false;
            for (int i = 0; i < gv_Main.DataRowCount; i++)
            {
                if ((bool)gv_Main.GetRowCellValue(i, "f_ck"))
                {
                    _str += gv_Main.GetRowCellValue(i, "f_id").ToString()+",";
                    if (gv_Main.GetRowCellValue(i, "f_typename").ToString() == "0")
                    {
                    }
                    else
                    {
                        _hasStuff = true;
                    }
                }
            }
            _str = _str.Substring(0, _str.Length-1);
            if (_hasStuff)
            {
                MessageBox.Show("该部门职位下还有未移出的人员，请先将该人员移出！");
            }
            else
            {
                TS_Win_Class.MySqlCom("update TS_S_T_P_Dep_Position set f_del=1,f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where  f_id in(" + _str + ")");
                _LoadOrgList();
                _LoadAuthGrid();
            }
        }
        private string GetFunclIst(TreeListNodes _Nodes, string _str)
        {
            string _Str = _str;
            foreach (TreeListNode node in _Nodes)
            {
                if ((bool)node.GetValue("f_ck"))
                {
                    _Str += node.GetValue("f_id").ToString() + ",";
                }
                if (node.HasChildren)
                {
                    _Str = GetFunclIst(node.Nodes, _Str);
                }
            }
            return _Str;
        }

        private void uc_Department_Position_Relation_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}